package org.example.unfiar;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public class SetTest {         // java.util.ConcurrentModificationException 并发修改异常
    // 测试Set线程安全性
    public static void main(String[] args) {

        // 解决方法：
        // 1. Set<String> set = Collections.synchronizedSet(oldSet); 工具类
        // 2. Set<String> set = new CopyOnWriteArraySet<>(); JUC 调用 Lock

        Set<String> set = new CopyOnWriteArraySet<>();

        for (int i = 1; i <= 30; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }
}
